package com.bls.service.impl;

import com.bls.action.BLSAction;
import com.bls.dao.AnalyseDao;
import com.bls.entity.Analyse;
import com.bls.entity.Draft;
import com.bls.service.AnalyseService;
import com.bls.service.DraftService;

import java.sql.Connection;

/**
 * (Analyse)表服务实现类
 *
 * @author makejava
 * @since 2022-01-10 18:42:18
 */
public class AnalyseServiceImpl implements AnalyseService {


    @Override
    public Analyse selectAnalyseById(long anylyseId) throws Exception {
        AnalyseDao analyseDao = BLSAction.getBean(AnalyseDao.class);
        final Connection connection = BLSAction.getDBConnection();
        return analyseDao.selectAnalyseById(connection, anylyseId);
    }

    @Override
    public int deleteById(long anylyseId) throws Exception {
        AnalyseDao analyseDao = BLSAction.getBean(AnalyseDao.class);
        final Connection connection = BLSAction.getDBConnection();
        return analyseDao.deleteById(connection, anylyseId);
    }

    @Override
    public void save(Long draftId, Analyse analyse) throws Exception {
        AnalyseDao analyseDao = BLSAction.getBean(AnalyseDao.class);
        final Connection connection = BLSAction.getDBConnection();
        DraftService draftService = BLSAction.getBean(DraftService.class);
        // todo 添加上事务控制
        // 1.先添加分析剧本，
        analyseDao.save(connection, analyse);
        // 2.更改对应draft的is_developed 和 analyseId
        Draft draft = new Draft();
        draft.setId(draftId);
        draft.setAnalyseId(analyse.getId());
        draft.setDeveloped(1);
        draftService.updateDraft(draft);
    }

    @Override
    public int updateAnalyse(Analyse analyse) throws Exception{
        AnalyseDao analyseDao = BLSAction.getBean(AnalyseDao.class);
        final Connection connection = BLSAction.getDBConnection();

        int i = -1;
        try {
            i = (int) analyseDao.updateIgnoreNull(connection, analyse);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }


}